import pygame

import entities

from config import CONFIG

from constants import UP,DOWN,LEFT,RIGHT

class Player(entities.Agent):
  def __init__(self,pos,size,image_set,sound_set):
    entities.Agent.__init__(self,pos,size,2,
                            CONFIG['Player Stats']['Speed'],
                            image_set,sound_set)
    
    self.normal_speed = CONFIG['Player Stats']['Speed']
    self.kit_speed    = self.normal_speed // 2
    
    self.jump_vel = list(CONFIG['Player Stats']['Jump'])
    
    v0 = 0
    v  = 0
    h  = 0
    
    while h < self.jump_vel[1]:
      v0 += 1
      v   = v0
      h   = 0
      
      while v > 0:
        h += v
        v -= 1
    
    self.jump_vel[1] = v0
    
  
  def get_image(self):
    image = None
    
    form = 'Normal'
    
    if self.health == 1:
      form = 'Kit'
    
    if   not self.collisions[DOWN]:
      if self.velocity[1] <= 0:
        image = self.image_set[form]['Jump']
      else:
        image = self.image_set[form]['Fall']
    elif self.status in ('Walk','Run'):
      image = self.image_set[form][self.status][self.frame]
    else:
      image = self.image_set[form][self.status]
    
    if self.left:
      image = pygame.transform.flip(image,True,False)
    
    return image
  
  def update_velocity(self):
    if self.collisions[DOWN]:
      if self.status == 'Walk':
        if self.left:
          if   self.velocity[0] > self.speed // -2:
            self.velocity[0] -= 1
          elif self.velocity[0] < self.speed // -2:
            self.velocity[0] += 1
        else:
          if   self.velocity[0] < self.speed // 2:
            self.velocity[0] += 1
          elif self.velocity[0] > self.speed // 2:
            self.velocity[0] -= 1
        
        # END if self.status == 'Walk'
      elif self.status == 'Run':
        if self.left:
          if   self.velocity[0] > -(self.speed):
            self.velocity[0] -= 4
          elif self.velocity[0] < -(self.speed):
            self.velocity[0] += 1
        else:
          if   self.velocity[0] < self.speed:
            self.velocity[0] += 4
          elif self.velocity[0] > self.speed:
            self.velocity[0] -= 1
        
        # END elif self.status == 'Run'
      else:
        if   self.velocity[0] < 0:
          self.velocity[0] += 1
        elif self.velocity[0] > 0:
          self.velocity[0] -= 1
        
        # END else (Meaning self.status != 'Walk' and self.status != 'Run')
      # END if self.collisions[DOWN]
    else:
      if self.status in ('Walk','Run'):
        if self.left:
          if self.velocity[0] > self.speed // -3:
            self.velocity[0] -= 1
        else:
          if self.velocity[0] < self.speed // 3:
            self.velocity[0] += 1
    
    entities.Entity.update_velocity(self)
  
  def update(self,controls,solids,baddies,items,widgets):
    if controls['Left'] or controls['Right']:
      self.left = controls['Left']
      
      if   controls['Run']:
        self.status = 'Run'
      else:
        self.status = 'Walk'
    else:
      self.status = 'Idle'
    
    if self.health == 1:
      self.speed = self.kit_speed
      
      if self.rect.width > 30:
        bottom  = self.rect.bottom
        centerx = self.rect.centerx
        
        self.rect = pygame.Rect(0,0,30,30)
        
        self.rect.bottom  = bottom
        self.rect.centerx = centerx
    else:
      self.speed = self.normal_speed
      
      if self.rect.width < 70:
        bottom  = self.rect.bottom
        centerx = self.rect.centerx
        
        self.rect = pygame.Rect(0,0,70,70)
        
        self.rect.bottom  = bottom
        self.rect.centerx = centerx
    
    if controls['Jump']:
      if self.collisions[DOWN]:
        self.velocity[1] = -(self.jump_vel[1])
        
        if self.health == 1:
          self.jump_vel[0] /= 2
        
        if self.left:
          self.velocity[0] -= self.jump_vel[0]
        else:
          self.velocity[0] += self.jump_vel[0]
        
        if self.health == 1:
          self.jump_vel[0] *= 2
    
    if self.status in ('Walk','Run'):
      self.frame_timer += 1
      
      if ((self.status == 'Walk' and self.frame_timer >= 3) or
          (self.status == 'Run'  and self.frame_timer >= 1)):
        
        self.frame       += 1
        self.frame_timer  = 0
        
        if self.frame >= 8:
          self.frame = 0
    
    entities.Agent.update(self,solids)
    
    if all(self.collisions):
      self.health -= 1
  
